package com.tan.androidtest.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;

/**
 * Created by Administrator on 2017/11/28.
 */

public abstract class LazyFragment extends Fragment {

    protected boolean isViewPrepared;//判断view是否初始化完成
    protected boolean isDataLoadCompelete;//数据是否加载完成
    private boolean isFirstVisibleToUser = true;

    // 该方法只有在ViewPager与Fragment结合使用的时候才会执行
    // 该方法在onCreateView之前调用
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (isFirstVisibleToUser && isViewPrepared && !isDataLoadCompelete) {
                isFirstVisibleToUser = false;
                onFirstVisibleToUser();
            }
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i("==--", "onActivityCreated");
        if (getUserVisibleHint()) {
            onFirstVisibleToUser();
        }
    }

    /**
     * 做懒加载的地方
     */
    protected abstract void onFirstVisibleToUser();

}
